package com.ruoyi.sxt.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SxtJuese;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sxt.domain.SxtGangwei;
import com.ruoyi.sxt.domain.SxtYonghu;
import com.ruoyi.sxt.domain.SxtYonghuGangwei;
import com.ruoyi.sxt.domain.SxtYonghuJuese;
import com.ruoyi.sxt.mapper.*;
import com.ruoyi.sxt.service.ISxtYonghuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-22
 */
@Service
public class SxtYonghuServiceImpl implements ISxtYonghuService
{
    @Autowired
    private SxtYonghuMapper sxtYonghuMapper;

    @Autowired
    private SxtJueseMapper sxtJueseMapper;

    @Autowired
    private SxtGangweiMapper sxtGangweiMapper;

    @Autowired
    private SxtYonghuJueseMapper sxtYonghuJueseMapper;

    @Autowired
    private SxtYonghuGangweiMapper sxtYonghuGangweiMapper;



    /**
     * 查询用户信息
     * 
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @Override
    public SxtYonghu selectSxtYonghuByUserId(Long userId)
    {
        return sxtYonghuMapper.selectSxtYonghuByUserId(userId);
    }

    /**
     * 查询用户信息列表
     * 
     * @param sxtYonghu 用户信息
     * @return 用户信息
     */
    @Override
    public List<SxtYonghu> selectSxtYonghuList(SxtYonghu sxtYonghu)
    {
        return sxtYonghuMapper.selectSxtYonghuList(sxtYonghu);
    }

    @Override
    public List<SxtJuese> selectSxtJueseByYonghuId(Long userId) {
        List<SxtJuese> sxtJuesesAll = sxtJueseMapper.selectSxtJueseList(new SxtJuese());
        List<Long> sxtJueseByYonghuIdList = sxtJueseMapper.selectSxtYonghuByRoleId(userId);
        if(sxtJuesesAll != null && sxtJuesesAll.size() > 0){
            for(SxtJuese sxtJuese : sxtJuesesAll){
                if(sxtJueseByYonghuIdList != null && sxtJueseByYonghuIdList.size() > 0){
                    for(Long jueseId : sxtJueseByYonghuIdList){
                        if(jueseId == sxtJuese.getRoleId()){
                            sxtJuese.setFlag(true);
                            break;
                        }
                    }
                }
            }
        }
        for(SxtJuese juese : sxtJuesesAll){
            if(juese.getRoleId() == 1){
                sxtJuesesAll.remove(juese);
                break;
            }
        }
        return sxtJuesesAll;
    }

    @Override
    @Transactional
    public void insertUserAuth(Long userId, Long[] roleIds) {
        sxtYonghuJueseMapper.deleteSxtYonghuJueseByUserId(userId);
        insertUserRole(userId, roleIds);
    }

    @Override
    public List<SxtGangwei> selectSxtGangweiByYonghuId(Long userId) {
        List<SxtGangwei> sxtGangweiListAll = sxtGangweiMapper.selectSxtGangweiList(new SxtGangwei());
        List<Long> sxtGangweiList = sxtGangweiMapper.selectSxtYonghuGangweiByGangweiList(userId);
        if(sxtGangweiListAll != null && sxtGangweiListAll.size() > 0){
            for(SxtGangwei sxtGangwei : sxtGangweiListAll){
                if(sxtGangweiList != null && sxtGangweiList.size() > 0){
                    for(Long gangweiId : sxtGangweiList){
                        if(gangweiId == sxtGangwei.getPostId()){
                            sxtGangwei.setFlag(true);
                            break;
                        }
                    }
                }
            }
        }
        return sxtGangweiListAll;
    }

    @Override
    public List<SxtYonghu> selectYonghuByJueseId(SxtJuese juese) {

        return sxtYonghuMapper.selectYonghuByJueseId(juese);
    }

    @Override
    public List<SxtYonghu> selectUnYonghuByJueseId(SxtJuese role) {
        return sxtYonghuMapper.selectUnYonghuByJueseId(role);
    }

    public void insertUserRole(Long userId, Long[] roleIds)
    {
        if (roleIds != null && roleIds.length > 0)
        {
            // 新增用户与角色管理
            List<SxtYonghuJuese> list = new ArrayList<SxtYonghuJuese>();
            for (Long roleId : roleIds)
            {
                SxtYonghuJuese ur = new SxtYonghuJuese();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0)
            {
                sxtYonghuJueseMapper.batchUserRole(list);
            }
        }
    }

    public void insertUserPost(Long userId, Long[] postIds)
    {
        if (postIds != null && postIds.length > 0)
        {
            // 新增用户与岗位管理
            List<SxtYonghuGangwei> list = new ArrayList<SxtYonghuGangwei>();
            for (Long postId : postIds)
            {
                SxtYonghuGangwei up = new SxtYonghuGangwei();
                up.setUserId(userId);
                up.setPostId(postId);
                list.add(up);
            }
            if (list.size() > 0)
            {
                sxtYonghuGangweiMapper.batchUserPost(list);
            }
        }
    }

    /**
     * 新增用户信息
     * 
     * @param sxtYonghu 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSxtYonghu(SxtYonghu sxtYonghu)
    {
        sxtYonghu.setCreateTime(DateUtils.getNowDate());
        int rows = sxtYonghuMapper.insertSxtYonghu(sxtYonghu);
        insertUserPost(sxtYonghu.getUserId(), sxtYonghu.getPostIds());
        insertUserRole(sxtYonghu.getUserId(), sxtYonghu.getRoleIds());
        return rows;
    }

    /**
     * 修改用户信息
     * 
     * @param sxtYonghu 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSxtYonghu(SxtYonghu sxtYonghu)
    {
        sxtYonghuGangweiMapper.deleteSxtYonghuGangweiByUserId(sxtYonghu.getUserId());
        insertUserPost(sxtYonghu.getUserId(), sxtYonghu.getPostIds());
        sxtYonghuJueseMapper.deleteSxtYonghuJueseByUserId(sxtYonghu.getUserId());
        insertUserRole(sxtYonghu.getUserId(),sxtYonghu.getRoleIds());
        sxtYonghu.setUpdateTime(DateUtils.getNowDate());
        return sxtYonghuMapper.updateSxtYonghu(sxtYonghu);
    }

    /**
     * 批量删除用户信息
     * 
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSxtYonghuByUserIds(String userIds)
    {
        return sxtYonghuMapper.deleteSxtYonghuByUserIds(Convert.toStrArray(userIds));
    }

    /**
     * 删除用户信息信息
     * 
     * @param userId 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSxtYonghuByUserId(Long userId)
    {
        return sxtYonghuMapper.deleteSxtYonghuByUserId(userId);
    }
}
